<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cu="http://apache.org/cocoon/counit" version="1.0">

  <!-- prepare input: make input files accessible -->

  <xsl:param name="counit-file-prefix">counit-file://</xsl:param>

  <xsl:template match="cu:parameter/@value">
    <xsl:attribute name="value">
      <xsl:call-template name="resolve-counit-files"/>
    </xsl:attribute>
  </xsl:template>
  
  <xsl:template match="cu:input//@*">
    <xsl:attribute name="{name()}" namespace="{namespace-uri()}">
      <xsl:call-template name="resolve-counit-files"/>
    </xsl:attribute>
  </xsl:template>
  
  <xsl:template match="cu:input//text()" name="resolve-counit-files">
    <xsl:choose>
      <xsl:when test="starts-with(., $counit-file-prefix)">
        <xsl:value-of select="ancestor::cu:*[@base][1]/@base"/>
        <xsl:text>.counit/</xsl:text>
        <xsl:value-of select="substring-after(., $counit-file-prefix)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="."/>        
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <!-- copy everything else -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>
